package ssm.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import ssm.mapper.PerMapper;
import ssm.mapper.PubMapper;
import ssm.mapper.UserMapper;
import ssm.myPojo.PerCustom;
import ssm.myPojo.PubCustom;
import ssm.myPojo.SourQueryVo;
import ssm.pojo.Per;
import ssm.pojo.Pub;
import ssm.pojo.User;
import ssm.service.UserService;

import java.util.Iterator;
import java.util.List;

public class UserServiceImpl implements UserService {


    @Autowired
    private PubMapper pubMapper;

    @Autowired
    private PerMapper perMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public User findUserByName(String name) throws Exception {

        List<User> users = userMapper.selectByName();
        for (User user : users) {
            if (user.getName().equals(name)) {
                System.out.println("用户名匹配成功");
                return user;
            }
        }
        System.out.println("失败");
        return null;
    }

    //通过用户id查找用户
    @Override
    public User findUserById(Integer id) throws Exception {

        User user = userMapper.selectByPrimaryKey(id);

        return user;
    }

    //修改用户信息
    @Override
    public void updateUser(User user) throws Exception {
        if(user!=null) {
            System.out.println("用户信息修改成功");
            userMapper.updateByPrimaryKey(user);
        }
    }

    //获取所有类型
    @Override
    public List<String> selectAllType() throws Exception {

        List<String> list = pubMapper.selectAllType();

        return list;
    }

    @Override
    public List<PubCustom> findAllPubByLike(SourQueryVo sourQueryVo) throws Exception {

        List<PubCustom> pubs = pubMapper.selectAllByLike(sourQueryVo);

        return pubs;
    }

    @Override
    public void signUser(String username,String pwd) throws Exception {
        User user = new User();
        user.setName(username);
        user.setPwd(pwd);

        userMapper.insert(user);
    }

    @Override
    public List<PubCustom> findPubByType(String Type) throws Exception {
        List<PubCustom> pubs = pubMapper.selectPubByType(Type);
        return pubs;
    }

    @Override
    public List<PerCustom> findPerByName(String name) throws Exception {
        System.out.println("查询收藏资源的用户名为"+name);
        List<PerCustom> pers = perMapper.selectPerByName(name);
        return pers;
    }

    @Override
    public List<PubCustom> findPubByName(String name) throws Exception {
        System.out.println("查询公开资源的用户名为"+name);
        List<PubCustom> pubs = pubMapper.selectPubByName(name);
        return pubs;
    }

    @Override
    public void insertIntoPub(PubCustom pubCustom) throws Exception {
        System.out.println("insert into pub !!!");
        pubMapper.insert(pubCustom);
        System.out.println("insert into pub success !!!");
    }
    @Override
    public void insertIntoPer(PerCustom perCustom) throws Exception {
        System.out.println("insert into per !!!");
        perMapper.insert(perCustom);
        System.out.println("insert into per success !!!");
    }
}
